/*
 * @Author: Lwz
 * @Coding: utf-8
 * @Date: 2019-12-20
 */

// 将在线图片转化成base64编码
export const convertImgUrlToBase64 = (url, outputFormat) => new Promise((resolve, reject) => {
   let img = document.createElement('img'); img.crossOrigin = 'Anonymous';
   let canvas = document.createElement('CANVAS');
   let ctx = canvas.getContext('2d');
   img.src = url;
   img.addEventListener('load', () => {
      canvas.height = img.height;
      canvas.width = img.width;
      ctx.drawImage(img, 0, 0);
      let dataURL = canvas.toDataURL(outputFormat || 'image/png');
      resolve(dataURL);
      canvas = null;
   });
});


// 生成 n 位的随机字符串，由大小写字母 + 数字构成
export const getRandomString = (n = 6) => {
   const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
   let string = '';
   for (let i = 0; i < n; i++) {
      let index = Math.floor(Math.random() * 62);
      string += chars.substr(index, 1);
   }
   return string;
};


// 判断浏览器及版本号【以内核为准，而非浏览器外壳】
export const judgeBrowserType = () => {
   const agent = navigator.userAgent;
   let browser = '',
version = '-1',
ver;
   switch (true) {
      case agent.includes('Opera'): // Opera浏览器(非Chromium内核, 老版本)
         browser = 'opera'; ver = agent.match(/Version\/([\d.]+)/)[1].split('.'); version = `${ver[0]}.${ver[1]}`;
         break;
      case agent.includes('Trident') || agent.includes('MSIE'): // IE浏览器 或 IE内核
         browser = 'ie';
         agent.includes('MSIE') && (ver = agent.match(/MSIE\/([\d.]+)/)[1].split('.'));
         !agent.includes('MSIE') && (ver = ['11', '0']);
         version = `${ver[0]}.${ver[1]}`;
         break;
      case agent.includes('Edge'): // Edge浏览器
         browser = 'edge'; ver = agent.match(/Edge\/([\d.]+)/)[1].split('.'); version = `${ver[0]}.${ver[1]}`;
         break;
      case agent.includes('Firefox'): // Firefox浏览器
         browser = 'firefox'; ver = agent.match(/Firefox\/([\d.]+)/)[1].split('.'); version = `${ver[0]}.${ver[1]}`;
         break;
      case agent.includes('Gecko') && !agent.includes('like Gecko'): // 非Firefox的Gecko内核, 无法判断版本
         browser = 'firefox';
         break;
      case agent.includes('Safari') && !agent.includes('Chrome'): // Safari浏览器
         browser = 'safari'; ver = agent.match(/Version\/([\d.]+)/)[1].split('.'); version = `${ver[0]}.${ver[1]}`;
         break;
      case agent.includes('Chrome') && agent.includes('Safari'): // Google Chrome 或 Chromium内核
         browser = 'chrome'; ver = agent.match(/Chrome\/([\d.]+)/)[1].split('.'); version = `${ver[0]}.${ver[1]}`;
         break;
      default:
         browser = 'others';
         break;
   }
   return { browser, version };
};

// 计算字符串的字节长度
export const strByteLen = (str) => {
   let count = 0;
   for (let i of str) {
      let code = i.charCodeAt();
      switch (true) {
         case code <= 0xff:
            count += 1; break;
         case code >= 0x100 && code <= 0xffff:
            count += 2; break;
         case code >= 0x10000 && code <= 0xffffff:
            count += 3; break;
         case code >= 0x1000000 && code <= 0xffffffff:
            count += 4; break;
      }
   }
   return count;
};

// 截取字符串的字节长度
export const strByteSlice = (str, length) => {
   let count = 0;
   for (let i = 0; i < str.length; i++) {
      let code = str.charCodeAt(i);
      switch (true) {
         case code <= 0xff:
            count += 1; break;
         case code >= 0x100 && code <= 0xffff:
            count += 2; break;
         case code >= 0x10000 && code <= 0xffffff:
            count += 3; break;
         case code >= 0x1000000 && code <= 0xffffffff:
            count += 4; break;
      }
      if (count > length) {
         return str.substring(0, i);
      }
   }
   return str;
};


/*
* 非事件处理的节流函数。例如在频繁触发的事件回调里，你只希望一部分操作节流，可以将这部分操作封装成函数，使用该节流函数
* 用例：
* clickHandle = () => {
*     this.operate1();
*     this.operate2();
*     throttleOfPart(this.operate3, 100); // 被节流的操作
* }
* */
export const throttleOfPart = (func, wait, ...rest) => {
   func.now = Date.now();
   !func.prev && (func.prev = func.now);
   if (func.now - func.prev > wait) {
      func(rest);
      func.prev = func.now;
   }
};

